package com.newcoin.kline.service.impl;

import com.newcoin.kline.dao.KlineDao;
import com.newcoin.kline.entity.*;
import com.newcoin.kline.service.KlineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
@Slf4j
public class KlineServiceImpl implements KlineService {

    @Autowired
    private KlineDao dao;

    @Override
    public KlineConfigEntity getConfig() throws Exception {
        log.info("获取配置");
        KlineConfigEntity entity = new KlineConfigEntity();
        List<String> list = new ArrayList<>();
        list.add("D");
        list.add("2D");
        list.add("3D");
        list.add("W");
        list.add("3W");
        list.add("M");
        list.add("GM");
        entity.setSupported_resolutions(list);
        List<ExchangeEntity> exchangeEntityList = new ArrayList<>();
        ExchangeEntity exchangeEntity = new ExchangeEntity();
        exchangeEntityList.add(exchangeEntity);
        entity.setExchanges(exchangeEntityList);
        List<SymbolsTypeEntity> symbols = dao.getTradingPair();
        entity.setSymbols_types(symbols);
        log.info("entity={}", entity);
        return entity;
    }

    @Override
    public Map<String, Object> getSymbol(String symbol) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("description", symbol);
        map.put("exchange-listed", "NewCoin");
        map.put("exchange-traded", "NewCoin");
        map.put("has_intraday", false);
        map.put("has_no_volume", false);
        map.put("minmov", 1);
        map.put("minmov2", 0);
        map.put("name", symbol);
        map.put("pointvalue", 1);
        map.put("pricescale", 100);
        map.put("session", "0930-1630");
        List<String> supported_resolutions = new ArrayList<>();
        supported_resolutions.add("D");
        supported_resolutions.add("2D");
        supported_resolutions.add("3D");
        supported_resolutions.add("W");
        supported_resolutions.add("3W");
        supported_resolutions.add("M");
        supported_resolutions.add("GM");
        map.put("supported_resolutions", supported_resolutions);
        map.put("ticker", symbol);
        map.put("timezone", "Asia/Shanghai");
        map.put("type", "stock");
        return map;
    }

    @Override
    public MarksEntity getMarks(String symbol, Long from, Long to, String resolution) throws Exception {
        log.info("标识入参 symbol={},from={},to={},resolution={}", symbol, from, to, resolution);
        MarksEntity marksEntity = new MarksEntity();
        List<String> colors = new ArrayList<>();
        colors.add("red");
        colors.add("blue");
        colors.add("green");
        colors.add("red");
        colors.add("blue");
        colors.add("green");
        marksEntity.setColor(colors);
        List<Long> ids = new ArrayList<>();
        ids.add(0L);
        ids.add(1L);
        ids.add(2L);
        ids.add(3L);
        ids.add(4L);
        ids.add(5L);
        marksEntity.setId(ids);
        List<String> labels = new ArrayList<>();
        labels.add("A");
        labels.add("B");
        labels.add("CORE");
        labels.add("D");
        labels.add("EURO");
        labels.add("F");
        marksEntity.setLabel(labels);
        List<String> labelFontColors = new ArrayList<>();
        labelFontColors.add("white");
        labelFontColors.add("white");
        labelFontColors.add("red");
        labelFontColors.add("#FFFFFF");
        labelFontColors.add("white");
        labelFontColors.add("#000");
        marksEntity.setLabelFontColor(labelFontColors);
        List<Integer> sizes = new ArrayList<>();
        sizes.add(14);
        sizes.add(28);
        sizes.add(7);
        sizes.add(40);
        sizes.add(7);
        sizes.add(14);
        marksEntity.setMinSize(sizes);
        List<String> texts = new ArrayList<>();
        texts.add("Today");
        texts.add("4 days back");
        texts.add("7 days back + Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.");
        texts.add("7 days back once again");
        texts.add("15 days back");
        texts.add("30 days back");
        marksEntity.setText(texts);
        List<Long> times = new ArrayList<>();
        times.add(1580559703L);
        times.add(1580559958L);
        times.add(1580560005L);
        times.add(1580560017L);
        times.add(1580560026L);
        times.add(1580560036L);
        marksEntity.setTime(times);
        return marksEntity;
    }

    @Override
    public List<TimescaleMarksEntity> getTimescaleMarks(String symbol, Long from, Long to, String resolution) throws Exception {
        log.info("时间刻度标记入参 symbol={},from={},to={},resolution={}", symbol, from, to, resolution);
        TimescaleMarksEntity<String> entity1 = new TimescaleMarksEntity();
        entity1.setColor("red");
        entity1.setId("tsm1");
        entity1.setLabel("A");
        entity1.setTime(1580515200L);
        entity1.setTooltip("");
        TimescaleMarksEntity<List<String>> entity2 = new TimescaleMarksEntity();
        entity2.setColor("blue");
        entity2.setId("tsm2");
        entity2.setLabel("D");
        entity2.setTime(1580169600L);
        List<String> toolTips1 = new ArrayList<>();
        toolTips1.add("Dividends: $0.56");
        toolTips1.add("Date: Tue Jan 28 2020");
        entity2.setTooltip(toolTips1);
        TimescaleMarksEntity<List<String>> entity3 = new TimescaleMarksEntity();
        entity3.setColor("green");
        entity3.setId("tsm3");
        entity3.setLabel("D");
        entity3.setTime(1579910400L);
        List<String> toolTips2 = new ArrayList<>();
        toolTips2.add("Dividends: $3.46");
        toolTips2.add("Date: Sat Jan 25 2020");
        entity3.setTooltip(toolTips2);
        TimescaleMarksEntity<List<String>> entity4 = new TimescaleMarksEntity();
        entity4.setColor("#999999");
        entity4.setId("tsm4");
        entity4.setLabel("E");
        entity4.setTime(1579219200L);
        List<String> toolTips3 = new ArrayList<>();
        toolTips3.add("Earnings: $3.44");
        toolTips3.add("Estimate: $3.60");
        entity4.setTooltip(toolTips3);
        TimescaleMarksEntity<List<String>> entity5 = new TimescaleMarksEntity();
        entity5.setColor("#999999");
        entity5.setId("tsm4");
        entity5.setLabel("E");
        entity5.setTime(1579219200L);
        List<String> toolTips4 = new ArrayList<>();
        toolTips4.add("Earnings: $5.40");
        toolTips4.add("Estimate: $5.00");
        entity5.setTooltip(toolTips4);
        List<TimescaleMarksEntity> list = new ArrayList<>();
        list.add(entity1);
        list.add(entity2);
        list.add(entity3);
        list.add(entity4);
        list.add(entity5);
        return list;
    }

    @Override
    public Map<String, Object> history(String symbol, String resolution, Long from, Long to) throws Exception {
        log.info("K柱入参 symbol={},resolution={},from={},to={}", symbol, resolution, from, to);
        List<BigDecimal> c = dao.getc(symbol, from, to);
        List<BigDecimal> o = dao.geto(symbol, from, to);
        List<BigDecimal> h = dao.geth(symbol, from, to);
        List<BigDecimal> l = dao.getl(symbol, from, to);
        List<BigDecimal> v = dao.getv(symbol, from, to);
        List<Long> t = dao.gett(symbol, from, to);
        Map<String, Object> map = new HashMap<>();
        if (c.size() == 0) {
            map.put("s", "no_data");
            map.put("nextTime", System.currentTimeMillis() / 1000L + 60);
            return map;
        }
        map.put("s", "ok");
        map.put("c", c);
        map.put("o", o);
        map.put("h", h);
        map.put("l", l);
        map.put("v", v);
        map.put("t", t);
        return map;
    }

    @Override
    public Map<String, Object> symbolInfo(String group) throws Exception {
        log.info("商品体系入参 group={}", group);
        Map<String, Object> map = new HashMap<>();
        List<String> symbols = new ArrayList<>();
        symbols.add("BTC/USDT");
        symbols.add("ETH/USDT");
        symbols.add("BCH/USDT");
        map.put("symbol", symbols);
        List<String> descriptions = new ArrayList<>();
        descriptions.add("BTC");
        descriptions.add("ETH");
        descriptions.add("BCH");
        map.put("description", descriptions);
        map.put("exchange-listed", "NewCoin");
        map.put("exchange-traded", "NewCoin");
        map.put("minmov", 1);
        map.put("minmov2", 0);
        List<Integer> pricescales = new ArrayList<>();
        pricescales.add(1);
        pricescales.add(1);
        pricescales.add(100);
        map.put("pricescale", pricescales);
        map.put("has-dwm", true);
        map.put("has-intraday", true);
        List<Boolean> has = new ArrayList<>();
        has.add(false);
        has.add(false);
        has.add(true);
        map.put("has-no-volume", has);
        List<String> types = new ArrayList<>();
        types.add("stock");
        types.add("stock");
        types.add("index");
        map.put("type", types);
        List<String> tickers = new ArrayList<>();
        tickers.add("BTC/USDT");
        tickers.add("ETH/USDT");
        tickers.add("BCH/USDT");
        map.put("ticker", tickers);
        map.put("timezone", "Asia/Shanghai");
        map.put("session-regular", "24x7");
        return map;
    }
}
